package com.mhkj.execute.plan.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.mhkj.common.util.ExportExcelUtils;
import com.mhkj.execute.plan.service.PlanNodeExecutionService;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 关键节点计划执行
 * 
 * @author wk
 * @date 2018年8月12日
 * @version 1.0
 */
@Controller
@RequestMapping("/keyPlanNodeExecution")
public class KeyPlanNodeExecutionController extends BaseController<PlanNode> {

	@Resource
	private PlanNodeExecutionService planNodeExecutionService;
	@Resource
	private DictionaryService dictionaryService;

	@Resource
	private JsonHandler jsonHandler;

	/**
	 * 月度工作计划
	 * 
	 * @param response
	 * @param planNode
	 * @param pageIndex
	 * @param pageSize
	 * @throws ParseException
	 */
	@RequestMapping("/listByPage")
	@ResponseBody
	public void listByPage(HttpServletResponse response, String yearMonth, PlanNode planNode, Long itemId,
			int pageIndex, int pageSize) throws ParseException {

		Dictionary dictionary = dictionaryService.getByValue("关键节点计划");
		planNode.setNodeProperty(dictionary);
		String firstDay = "";
		String lastDay = "";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		if (yearMonth != null) {

			Date formattime = new SimpleDateFormat("yyyy-MM").parse(yearMonth);

			firstDay = format.format(formattime.getTime());
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(formattime);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
			lastDay = format.format(calendar.getTime());
		}
		Pagination<PlanNode> pagination = planNodeExecutionService.listByPage(planNode, firstDay, lastDay, itemId,
				pageIndex, pageSize);

		jsonHandler.sendJsonString(response, true, "获取数据成功", pagination,
				new String[] { "id", "plan.name", "business.typeName", "plan.planType.value", "nodeName",
						"nodeProperty.value", "completeStandard", "rate", "state", "beginDate", "endDate", "postName",
						"remark", "riskState" },
				new String[] { "id", "planName", "businessName", "planTypeName", "nodeName", "nodePropertyName",
						"completeStandard", "rate", "state", "beginDate", "endDate", "postName", "remark",
						"riskState" });
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see 新增节点
	 */
	@Override
	@RequestMapping("/save")
	@ResponseBody
	public ResponseMessage save(PlanNode t) {
		String message = planNodeExecutionService.save(t);
		if (message == null) {
			return ResponseMessage.createMessage(true, "新增计划节点成功！");
		} else {
			return ResponseMessage.createMessage(false, message);
		}
	}

	/**
	 * 查看节点详情
	 */
	@RequestMapping("/info")
	@ResponseBody
	public void info(HttpServletResponse response, long id) {
		PlanNode planNode = planNodeExecutionService.getById(id);

		jsonHandler.sendJsonString(response, true, "获取数据成功", planNode,
				new String[] { "id", "plan.name", "plan.item.name", "business.typeName", "plan.planType.value",
						"nodeName", "nodeProperty.value", "completeStandard", "rate", "state", "beginDate", "endDate",
						"postName", "remark" },
				new String[] { "id", "planName", "itemName", "businessName", "planTypeName", "nodeName",
						"nodePropertyName", "completeStandard", "rate", "state", "beginDate", "endDate", "postName",
						"remark" });

	}

	/**
	 * 获取导出
	 * 
	 * @author
	 * @date 2018年5月22日
	 * @version 1.0
	 */
	@RequestMapping("/exportExcel")
	public Map<String, Object> exportExcel(HttpServletResponse response, String yearMonth, PlanNode planNode,
			Long itemId, int pageIndex, int pageSize) throws ParseException {

		Dictionary dictionary = dictionaryService.getByValue("关键节点计划");
		planNode.setNodeProperty(dictionary);

		String firstDay = "";
		String lastDay = "";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		if (yearMonth != null) {

			Date formattime = new SimpleDateFormat("yyyy-MM").parse(yearMonth);

			firstDay = format.format(formattime.getTime());
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(formattime);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
			lastDay = format.format(calendar.getTime());
		}
		Pagination<PlanNode> pagination = planNodeExecutionService.listByPage(planNode, firstDay, lastDay, itemId,
				pageIndex, pageSize);

		List<PlanNode> objList = pagination.getData();

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (objList != null && objList.size() > 0) {
			Map<String, Object> map = null;
			PlanNode bean = null;
			for (int i = 0, size = objList.size(); i < size; i++) {
				bean = objList.get(i);
				map = beanToMap(bean);

				map.put("planNodeName", objList.get(i).getPlan().getName());
				map.put("rate", objList.get(i).getRate() + "%");
				String stateValue = "";
				if (objList.get(i).getRiskState() == 1) {
					stateValue = "有风险";
				} else if (objList.get(i).getRiskState() == 2) {
					stateValue = "失控";
				} else {
					if (objList.get(i).getState() == 1) {
						stateValue = "未开始";
					} else if (objList.get(i).getState() == 2) {
						stateValue = "进行中";
					} else if (objList.get(i).getState() == 3) {
						stateValue = "有风险";
					} else if (objList.get(i).getState() == 4) {
						stateValue = "失控";
					} else if (objList.get(i).getState() == 5) {
						stateValue = "已完成";
					} else if (objList.get(i).getState() == 6) {
						stateValue = "延期完成";
					} else if (objList.get(i).getState() == 7) {
						stateValue = "逾期";
					} else if (objList.get(i).getState() == 8) {
						stateValue = "节点暂停";
					} else if (objList.get(i).getState() == 9) {
						stateValue = "节点取消";
					}
				}

				map.put("businessName", objList.get(i).getBusiness().getTypeName());
				map.put("state", stateValue);
				map.put("no", i + 1);

				list.add(map);
			}
		}
		ExportExcelUtils excel = new ExportExcelUtils();
		String title = "编号,计划名称,业务板块,节点名称,完成标准,状态,进度,计划开始时间,计划结束时间,负责人";
		String key = "no,planNodeName,businessName,nodeName,completeStandard,state,rate,beginDate,endDate,postName";
		excel.exportData(list, title, key, "关键节点计划信息", response);

		return null;
	}

	public static <T> Map<String, Object> beanToMap(T bean) {
		Map<String, Object> map = Maps.newHashMap();
		if (bean != null) {
			BeanMap beanMap = BeanMap.create(bean);
			for (Object key : beanMap.keySet()) {
				map.put(key + "", beanMap.get(key));
			}
		}
		return map;
	}
}
